package com.docman.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.docman.dao.CommentDAO;
import com.docman.dao.DocumentDAO;
import com.docman.domainmodel.CommentBean;
import com.docman.domainmodel.DocumentBean;


@WebServlet("/DocumentInfoServlet")
public class DocumentInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public DocumentInfoServlet() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("text/html;  charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		
		String docId = request.getParameter("docId");
		int id = Integer.parseInt(docId);
		
		System.out.println(id);
		
		DocumentBean doc = DocumentDAO.getDocInfo(id);
		double uploaderRatingInfo = DocumentDAO.getUploaderRating(id);
		ArrayList<CommentBean> comments = CommentDAO.getComment(id);
		
		request.setAttribute("document", doc);
		request.setAttribute("uploaderRating", uploaderRatingInfo);
		request.setAttribute("comments", comments);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/documentPage.jsp");

		  if (dispatcher != null){
			  //System.out.println(docs.toString());
		  dispatcher.forward(request, response);

		  }
		
	}

}
